<?
include("../../inc/class/config.php");		// загружаем классы настроек
$cfg=new Tconfig;
include("../../config.php");			// загружаем основные настройки, которых нет в БД
include("../../inc/connect.php");			// соеденяемся с БД, получаем $cfg->base_id


$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
$oper= $_POST['oper'];
$curuserid = $_GET['curuserid'];
$id = $_POST['id'];

// в список пользователей БП, добавляем номер БП, по его randomid
 $SQL="SELECT * FROM bp_accept WHERE status=1";
 $result = mysql_query( $SQL ) or die("Не могу выбрать пользователей БП!".mysql_error());
 while($row = mysql_fetch_array($result)) {
   $bpid=$row[id];  
   $bprid=$row[randomid];  
   $SQL="UPDATE bp_userlist SET bpid='$bpid' WHERE randomid='$bprid'";
   $result2 = mysql_query( $SQL ) or die("Не могу обновить id БП!".mysql_error());
 };
//


if ($oper=='')
{
	if(!$sidx) $sidx =1;
	$result = mysql_query("SELECT COUNT(*) AS count FROM bp_userlist WHERE status=0 and userid='$curuserid'");
	$row = mysql_fetch_array($result);
	$count = $row['count'];

	if( $count >0 ) {$total_pages = ceil($count/$limit);} else {$total_pages = 0;};
	if ($page > $total_pages) $page=$total_pages;

	$start = $limit*$page - $limit;
	$SQL = "SELECT bp_accept.id AS id,bp_userlist.id AS uid, bp_userlist.dtstart AS dtstart, bp_accept.title AS title
FROM bp_userlist
INNER JOIN bp_accept ON bp_userlist.bpid = bp_accept.id WHERE bp_userlist.status=0 and bp_userlist.userid='$curuserid' ORDER BY bp_accept.id DESC, $sidx $sord LIMIT $start , $limit ";
        //echo "!$SQL!";            
	$result = mysql_query( $SQL ) or die("Не могу выбрать сформировать список bp_userlist!".mysql_error());

	$responce->page = $page;
	$responce->total = $total_pages;
	$responce->records = $count;
	$i=0;
	while($row = mysql_fetch_array($result)) {
	    $responce->rows[$i]['id']=$row[uid];    
            $responce->rows[$i]['cell']=array($row[uid],$row[dtstart],$row[title]);
	    $i++;
	}
	echo json_encode($responce);
};

?>